package servlets;

import bean.Goods;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        response.setContentType("text/html;charset=utf-8");
        if(session.getAttribute("name")==null){
            response.getWriter().write("您还没有登录，不能进入购物车！三秒后转向登录页面");
            response.setHeader("Refresh","3;URL=/login.html");
        }else {
            Goods goods1=new Goods("1","小米手机",1999.00,1);
            Goods goods2=new Goods("2","苹果手机",6999.00,1);
            List<Goods> goodses=new ArrayList<Goods>();
            goodses.add(goods1);
            goodses.add(goods2);
            session.setAttribute("cart",goodses);
            Cookie cookie=new Cookie("JSESSIONID",session.getId());
            response.addCookie(cookie);
            response.getWriter().write(session.getAttribute("name")+",您已经选购了两件商品<a/>");
            response.getWriter().write("<a href='/pay'>进入支付");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
